package h.d.bluetooth.visitor.db;

import h.d.bluetooth.visitor.common.VaE;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * @author hdeom
 * @date 2014. 1. 15.
 *
 */
public class BVDBHelper extends SQLiteOpenHelper{


	public static final String TABLE_DEVICE_INFO = "DEVICEINFO";
	public static final String TABLE_DEVICE_LOG = "DEVICELOG";

	public static final String COL_INFO_DATE = "DATE";
	public static final String COL_INFO_NAME = "NAME";
	public static final String COL_INFO_FIXNAME = "FIXNAME";
	public static final String COL_INFO_MACADDRESS = "MACADDRESS";
	public static final String COL_INFO_RSSI = "RSSI";

	private Context mContext;
	private static BVDBHelper mInstance = null;

	public static BVDBHelper getInstance(Context context)
	{
		if(mInstance == null)
		{
			mInstance = new BVDBHelper(context);
		}
		return mInstance;
	}



	/**
	 * @param context
	 */
	public BVDBHelper(Context context){
		super(context, VaE.DBFILENAME, null, VaE.DBVERSION);
		mContext = context;
	}


	@Override
	public void onCreate(SQLiteDatabase db) {

		init(db);
	}

	@Override
	public synchronized void close() {
		// TODO Auto-generated method stub
		if (mInstance != null)
		{
			mInstance = null;
		}
		super.close();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEVICE_INFO);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEVICE_LOG);
		onCreate(db);
	}

	/**
	 * @author hdeom
	 * @date 2014. 1. 15.
	 * @param
	 * @return void
	 *
	**/
	public void init(SQLiteDatabase db){
		db.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE_DEVICE_INFO +" ("
				+ COL_INFO_NAME + " TEXT,"
				+ COL_INFO_FIXNAME + " TEXT,"
				+ COL_INFO_MACADDRESS + " TEXT"
				+ ");");

		db.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE_DEVICE_LOG +" ("
				+ COL_INFO_DATE + " TEXT,"
				+ COL_INFO_NAME + " TEXT,"
				+ COL_INFO_MACADDRESS + " TEXT,"
				+ COL_INFO_RSSI + " INTEGER"
				+ ");");
	}



}
